<?php

namespace App\Console\Commands\Plane\PlaneOrder;

use App\Models\Plane\PlaneOrders\PlaneOrder;
use App\Models\Plane\PlaneOrders\Repositories\Interfaces\PlaneOrderRepositoryInterface;
use Exception;
use Illuminate\Console\Command;
use Log;

class TicketReturningQuery extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ticket_return:query';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'API查询退票中的订单状态是否退票退款成功';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $planeOrderRepo   = app(PlaneOrderRepositoryInterface::class);
        $returning_orders = $planeOrderRepo->getReturningOrders();
        /**
         * @var PlaneOrder $order
         */
        foreach ($returning_orders as $order) {
            if ($order->getOrderService() == 'qunar') {
                $return_info = $order->returns->first();
                $service     = $order->OrderStrategy();
                $od          = $service->refund_detail($order, $return_info);
                if ($od->isReturned()) {
                    $provider_return_fee = $od->getProviderReturnedFee();
                    try {
                        $return_info->ticket_return_success([
                            'provider_refund_price' => $provider_return_fee,
                        ]);
                    } catch (Exception $e) {
                        Log::error("订单:{$order->getOrderSn()},退票失败:{$e->getMessage()}");
                    }
                }
            }
        }
    }
}
